$(function(){
    $(".main .menu li ").click(function(){
        $(this).addClass('on').siblings().removeClass('on');
        var index = $(this).index();
        if(index == '0'){
            $('.mobLogin').hide();
            $('.oLogin').show();
        }else if(index == '1') {
            $('.mobLogin').show();
            $('.oLogin').hide();
        }
    });
});
//检查个人普通用户登录
function checkPersonal(oForm){

    var username = str_trim($(oForm).find('#username').val());
    if(username == ''){
        layerMsg('用户名/手机号码不能为空!',2);
        $(oForm).find('#username').focus();
        $(oForm).find('#username').val(username);
        return false;
    }
    if($(oForm).find('#password').val() == '' ||  $(oForm).find('#password').val().length < 6  ){
        layerMsg('密码不能少于6位!',2);
        $(oForm).find('#password').focus();
        return false;
    }
    var queryString = $(oForm).serialize();
    $.ajax({
        url: '/mobile/personal/ajax_login',
        dataType: "json",
        data: queryString,
        type: "POST",
        success: function (data) {
            if(data.status == '0'){
                layerMsg(data.msg,1);
                location = '/mobile/personal/welcome';
            }else{
                layerMsg(data.msg,2);
            }
        },
        error: function (data) {
            var msgErr='';
            var errors = $.parseJSON(data.responseText);
            $.each(errors, function(index, value) {
                msgErr += "<p style='line-height: .4rem; width: 100%'>"+ value+";</p>";
            });
            layerMsg(msgErr,2);
        },
        complete: function () {
        }
    });
    return false;
}
//微信手机绑定
function checkPersonalByWeiChat(oForm){

    var username = str_trim($(oForm).find('#username').val());
    if(username == ''){
        layerMsg('用户名/手机号码不能为空!',2);
        $(oForm).find('#username').focus();
        $(oForm).find('#username').val(username);
        return false;
    }
    if($(oForm).find('#password').val() == '' ||  $(oForm).find('#password').val().length < 6  ){
        layerMsg('密码不能少于6位!',2);
        $(oForm).find('#password').focus();
        return false;
    }
}
//检查个人手机登录
function checkMobile(oForm){
    var mobile = str_trim($(oForm).find('#mobile').val());
    if(mobile == ''){
        layerMsg('请输入手机号码!',2);
        $(oForm).find('#mobile').focus();
        $(oForm).find('#mobile').val(mobile);
        return false;
    }else{
        var regu =/(^1[3-9]{1}[0-9]{9}$)/;
        if(!regu.test(mobile)){
            layerMsg('请输入正确的手机号码!',2);
            $(oForm).find('#mobile').focus();
            $(oForm).find('#mobile').val(mobile);
            return false;
        }
    }
    var mobCode = str_trim($(oForm).find('#mobCode').val());
    if(mobCode == ''){
        layerMsg('请输入验证码!',2);
        $(oForm).find('#mobCode').focus();
        $(oForm).find('#mobCode').val(mobCode);
        return false;
    }else{
        var mobileRegUrl =  '/mobile/personal/welcome';//成功后向的地址
        if($(oForm).find('#openid').val() == '' ||  $(oForm).find('#openid').val() == undefined || $(oForm).find('#openid').length() < 0 ){
            var loginUrl='/mobile/personal/ajax_mobile_login';//正常登陆
        }else{
            return true;
            //var loginUrl='/mobile/personal/stroybindingwithmobile';//登陆并邦定微信
        }
        $.post('/mobile/personal/ajax_sms_url',{'_token':_token,'mobile':mobile,'mobCode':mobCode},function(data) {
            if(data == "true"){
                var queryString = $(oForm).serialize();
                $.ajax({
                    url: loginUrl,
                    dataType: "json",
                    data: queryString,
                    type: "POST",
                    success: function (data) {
                        if(data.status == '0'){
                            layerMsg(data.msg,2);
                            location = mobileRegUrl;
                        }else{
                            layerMsg(data.msg,2);
                        }
                    },
                    error: function (data) {
                        var msgErr='';
                        var errors = $.parseJSON(data.responseText);
                        $.each(errors, function(index, value) {
                            msgErr += "<p style='line-height: .4rem; width: 100%'>"+ value+";</p>";
                        });
                        layerMsg(msgErr,2);
                    },
                    complete: function () {
                    }
                });
            }else{
                layerMsg('验证码和手机不匹配!',2);
                $(oForm).find('#mobCode').focus();
                $(oForm).find('#mobCode').val(mobCode);
                return false;
            }
        });
    }
    return false;
}

//点击获取手机验证码
var mobTime;
function getModCode(othis,objMobile) {
    var mobile = str_trim($(objMobile).val());
    if (mobile != '') {
        var mobValue = str_trim(mobile);
        var regu = /(^1[3-9]{1}[0-9]{9}$)/;
        if (regu.test(mobValue)) {

            var src= 'this.src="'+imgCode+'" + Math.random()';
            var docSrc = 'document.getElementById("imgCodes").src="'+imgCode+'" + Math.random()';
            var str = "";
            str += "<div class='addCode'>";
            str += "<form method='post' name='mForm' id='mForm' onsubmit='return false'>";
            str += "<span><input name='code' id='code' type='text'  placeholder='验证码' class='code' ></span>";
            str += "<span><img id='imgCodes' src='" + imgCode + "' onclick='"+ src + "' /></span>";
            str += "<span class='change'><a onclick='"+ docSrc + "'>换一换</a></span>";
            str += "<div class='codeErr' id='codeErr'></div>";
            str += "</form>";
            str += "</div>";

            layer.open({
                title: [
                    '请输入验证码',
					'background-color:#00c6c9; color:#fff; height:.8rem; line-height:.8rem'
                ]
                ,className : 'loginCode' //自定风格
                ,content: str
                ,shadeClose:false
				,style: 'font-family:微软雅黑; font-size:.28rem'
                ,btn: ['确认', '取消']
                ,yes: function(index){
                    var code = $("#mForm").find("#code").val();
                    $.post('/common/ajax_code',{'_token':_token,'code':code},function(data) {
                        if(data == 'true'){
                            $.post('/mobile/personal/ajax_mob_login_code',{'_token':_token,'mobile':mobValue,'code':code},function(data){
                                if(data.status==2){
                                    //验证手机号码是不是存在
                                    layerMsg(data.msg,2);
                                }else if(data.status==1){
                                    //获取验证码成功
                                    layerMsg(data.msg,2);
                                    var iNum=60;
                                    mobTime = setInterval(function(){
                                        iNum -= 1;
                                        othis.attr("disabled",true).val('('+iNum+')秒后重新获取').css({"background":"#ccc","color":"#333"});
                                        if(iNum <=0){
                                            othis.removeAttr("disabled").val('获取验证码').css({"background":"#505962","color":"#fff"});
                                            clearInterval(mobTime);
                                        }
                                    },1000);
                                }else if(data.status==0){
                                    //验证码获取失败
                                    layerMsg(data.msg,2);
                                }
                            });
                        }else{
                            $("#mForm").find("#codeErr").html('验证码错误');
                        }
                    });
                }
            });
        } else {
            objMobile.val(str_trim(mobile));
            layerMsg('请输入正确的手机号码!', 2);
        }
    }else{
        objMobile.val(str_trim(mobile));
        layerMsg('请填写手机号码!', 2);
    }
}
